package cn.easybuy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.Product_categoryDao;
import cn.easybuy.entity.Product_category;
import cn.easybuy.utils.DataSourceUtil;

public class Product_categoryDaoImpl extends BaseDao implements Product_categoryDao {
	ResultSet rs=null;

	public Product_categoryDaoImpl(Connection conn) {
		super(conn);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Product_category> getList(String type) {
		List<Product_category> list=new ArrayList<Product_category>();
		String sql="select * from easybuy_product_category where type=?";
		Object[]objArr = {type};
		try {
			//调用方法获得结果集
			rs=this.executeQuery(sql, objArr);
			//将结果集转换为对象
			while(rs.next()){
				list.add(this.rsToPc(rs));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DataSourceUtil.closeAll(null, null, rs);
		}
		return list;
	}

	private Product_category rsToPc(ResultSet rs){
		Product_category pc=null;
		try {
			pc=new Product_category();
			pc.setId(rs.getInt("id"));
			pc.setName(rs.getString("name"));
			pc.setParentId(rs.getInt("parentid"));
			pc.setType(rs.getInt("type"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pc;
	}
	@Override
	public int delCategorys(String id) {
		String sql="delete from easybuy_product_category where id=?";
		Object[]objArr = {id};
		return  this.executeUpdate(sql, objArr);
	}

	@Override
	public int addCategory(Product_category pc) {

		String sql="INSERT INTO easybuy_product_category(NAME,parentId,TYPE) VALUES(?,?,?)";
		Object[]objArr = {pc.getName(),pc.getParentId(),pc.getType()};
		return this.executeUpdate(sql, objArr);
	}

	@Override
	public Product_category getProductCategoryById(String id) {
		Product_category pc=null;
		String sql="select * from easybuy_product_category where id=?";
		Object[]objArr = {id};
		//调用方法获得结果集
		try {
			rs=executeQuery(sql, objArr);
			if(rs.next()){
				pc=this.rsToPc(rs);
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		finally{
			DataSourceUtil.closeAll(null, null, rs);
		}
		return pc;
	}

	@Override
	public int updateCategory(Product_category pc) {
		String sql="UPDATE easybuy_product_category SET NAME=?,parentId=? WHERE id=?";
		Object[]objArr = {pc.getName(),pc.getParentId(),pc.getId()};
		return this.executeUpdate(sql, objArr);
	}

	@Override
	public int checkChildCategory(String id) {
		String sql="select count(*) from easybuy_product_category where parentId=?";
		Object[]objArr = {id};
		int count = 0;
		try {
			rs=super.executeQuery(sql,objArr);
			if(rs.next()){
				count = rs.getInt("count(*)");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DataSourceUtil.closeAll(null, null, rs);
		}
		return count;
	}

	@Override
	public List<Product_category> getCategoryList(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		String sql = "select * from easybuy_product_category limit ?,?";
		Object[]objArr = {(pageNo-1)*pageSize,pageSize};
		rs=executeQuery(sql, objArr);
		List<Product_category>list = new ArrayList();
		Product_category pc=null;
		try {
			while(rs.next()){
				pc=new Product_category();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentid"));
				pc.setType(rs.getInt("type"));
				list.add(pc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DataSourceUtil.closeAll(null, null, rs);
		}
		return list;
	}

	@Override
	public int getCount() {
		int i = 0;
		String sql = "select count(*) from easybuy_product_category";
		rs = this.executeQuery(sql, null);
		try {
			while(rs.next()){
				i=rs.getInt("count(*)");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DataSourceUtil.closeAll(null, null, rs);
		}
		return i;
	}
}
